/*
 * TicTacToeController.fx
 *
 * Created on Apr 27, 2009, 2:24:27 AM
 */

package com.dg.gaming.tictactoe.client.fx;
import com.dg.gaming.client.fx.gui.DGGameController;
import com.dg.gaming.tictactoe.client.fx.TicTacToeBoardComponent;
import com.dg.gaming.tictactoe.common.MsgTicTacToeMove;
import com.dg.gaming.tictactoe.common.TicTacToeModel;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/**
 * @author dito
 */

public class TicTacToeController extends DGGameController{
 public  var tmodel:TicTacToeModel ;
 public  var boardComponent:TicTacToeBoardComponent ;

 
 public  override  function start(){
     tmodel.reset();
 }
 public override function mousePressed(e:MouseEvent) {
       /*var board1 = boardComponent.getBoardCoords(e.x, e.y);
       if (board1.x >= 0 and board1.x < 3 and board1.y >= 0 and board1.y < 3) {
           tmodel.setData(board1.x, board1.y, TicTacToeModel.O);
           
       }*/

        if (isGamePlaying() and isThisPlayersTurn()) { // ensure game has started
            // get board point from screen position of mouse click
            var board = boardComponent.getBoardCoords(e.x, e.y);

            // ensure board point is in range (0 to 2)
            if (board.x >= 0 and board.x < 3 and board.y >= 0 and board.y < 3) {

                // check model at this point is BLANK and it is this persons go
                if (tmodel.getData(board.x, board.y) == TicTacToeModel.BLANK) {
                    var value = getCurrentPlayerSeatNum();
                    move(board.x, board.y, value);
                }
            }
        }
    }
        public function move(x:Integer, y:Integer, value:Integer) {
        // update model and change player turn
        tmodel.setData(x, y, value);
        // send move to other user
        var move = new MsgTicTacToeMove
                                 (x, y, value);
        // next player turn.
        nextPlayer(move);


//        conn.send(move);
    }

}
